$().ready(function () {
    validateRule();
    $(document).ready(function () {//通用方法去输入框去除空格
        $("form").on("change", function () {
            $("form input[type=text]").each(function () {
                this.value = this.value.replace(/\s+/g, '');
            });
        })
    });
    var channeltype = $('#accounttypehiden').val();
    var username = $('#username').val();
    if (username != null && username != '') {
        document.getElementById("resetPasswd").style.display = "inline";
    }

    if (channeltype != null && channeltype != '') {
        $('#accounttype').val(channeltype);
    }
    $("#username").change(function () {
        var userName = $("#username").val();
        var url = "/market/account/findByUserName?username=" + userName;
        $.ajax({
            url: url,
            type: 'post',
            success: function (data) {
                if (0 != data) {
                    layer.alert("该登录账号已存在，请重新输入");
                    $("#username").val('');
                    return false;
                }
            }
        })
    })
});

$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});

function resetPassword() {
    debugger;
    var prefixaa = "/market/account";
    layer.confirm("确认要重置密码？", {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefixaa + "/resetPasswd",
            type: "post",
            async: false,
            data: $('#signupForm').serialize(),
            success: function (r) {
                if (r.code == 0) {
                    parent.layer.msg("操作成功");
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);

                } else {
                    parent.layer.alert(data.msg)
                }
            }
        });
    })
}

function update() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/market/account/save",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            },
        },
        messages: {
            username: {
                required: icon + "请输入名字"
            },
            password: {
                required: icon + "请输入密码"
            },
        }
    })
}